package fr.labri.harmony.analysis.jsep.results.filter;

import java.io.IOException;
import java.io.PrintStream;
import java.sql.ResultSet;
import java.sql.SQLException;

import fr.labri.harmony.analysis.jsep.results.LoadRules;

public class MigrationClassifier extends ResultFilter {

	@Override
	public void computeResult(ResultSet rs) throws SQLException, IOException {
		rs.first();
		PrintStream ps = new PrintStream("Rules_data.arff");
		
		ps.println("@RELATION migration_rules");
		ps.println("@ATTRIBUTE source string");
		ps.println("@ATTRIBUTE target string");
		ps.println("@ATTRIBUTE nb_devs numeric");
		ps.println("@ATTRIBUTE distance_files numeric");
		ps.println("@ATTRIBUTE length numeric");
		ps.println("@ATTRIBUTE hours numeric");
		ps.println("@ATTRIBUTE migration {0,1}");
		
		while (rs.next()){			
			String source = rs.getString("NAME");
			String target = rs.getString("MIGRATION_LIB_TO");
			if(target!=null) {
				int nbdev = rs.getInt("migration_devs_contrib");
				int distance_files = Math.abs(rs.getInt("migration_nb_fichiers_in") - rs.getInt("migration_nb_fichiers_out"));
				int length = rs.getInt("migration_length");
				int hours = Utils.getHoursDiff(rs.getLong("migration_timestamp_in")*1000, rs.getLong("timestamp_out")*1000);
				ps.print(source+";"+target+";"+nbdev+";"+distance_files+";"+length+";"+hours);
				if(LoadRules.isTrueRule(source, target)) {
					ps.println(";1");
				}
				else {
					ps.println(";0");
				}
			}
		}
		ps.close();
	}

}
